/* Copyright (c) 2011 by Mani Brundha Kattamuri, Haritha Subramanyam
 *
 * Project Info:  http://code.google.com/p/andriod-travelapp-csc780/wiki/Design
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
 * USA.
 */
package com.example.TravelApp.search.common;

import java.util.ArrayList;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;
import com.example.TravelApp.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

/**
 * This class is responsible for plotting the overlay items on the map view It
 * contains an array list of overlay items to which each of the locations are
 * added and plotted. A marker is placed on each of these locations and text
 * denoting the location is placed at each overlay
 * 
 * @author Haritha Subramanyam, Mani Brundha
 * 
 */
public class PlotItemizedOverlay extends ItemizedOverlay {

	// List for overlay items added to the map
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Context mContext;
	private int mTextSize;

	/**
	 * Constructs an overlay and adds a marker over the point
	 * 
	 * @param defaultMarker
	 *            - marker to be placed over the overlay
	 */

	public PlotItemizedOverlay(Drawable defaultMarker) {
		super(boundCenterBottom(defaultMarker));
	}

	/**
	 * Constructs an overlay and adds a marker over the point and sets the
	 * context
	 * 
	 * @param defaultMarker
	 *            - marker to be placed over the overlay
	 * @param context
	 *            - current context
	 */
	public PlotItemizedOverlay(Drawable defaultMarker, Context context) {
		super(boundCenterBottom(defaultMarker));
		mContext = context;
	}

	/**
	 * Constructs an overlay and adds a marker over the point and sets the
	 * context Also sets the text size to be used for overlay item description
	 * on map
	 * 
	 * @param defaultMarker
	 *            - marker to be placed over the overlay
	 * @param context
	 *            - current context
	 */
	public PlotItemizedOverlay(Drawable defaultMarker, Context context,
			int textSize) {
		super(defaultMarker);
		mContext = context;
		mTextSize = textSize;
	}

	/**
	 * Adds an overlay item to the array list of overlay items populate method
	 * is called to populate the newly added overlay
	 * 
	 * @param overlay
	 *            - new overlay item
	 */
	public void addOverlay(OverlayItem overlay) {
		mOverlays.add(overlay);
		populate();
	}

	/**
	 * Overloaded addOverlay method Sets the specified marker and calls the
	 * addOverlay method to add the overlay to the list
	 * 
	 * @param overlay
	 * @param marker
	 */
	public void addOverlay(OverlayItem overlay, Drawable marker) {
		overlay.setMarker(boundCenterBottom(marker));
		addOverlay(overlay);
	}

	/**
	 * Removes an overlay from the list
	 * 
	 * @param index
	 *            - index of the overlay item to be removed
	 */
	public void removeOverlay(int index) {
		mOverlays.remove(index);
		populate();
	}

	/**
	 * Returns an overlay item at the specified index Used for creating the
	 * overlay
	 */
	@Override
	protected OverlayItem createItem(int i) {
		return mOverlays.get(i);
	}

	/**
	 * Returns the size of the arraylist that contains the overlay items
	 */
	@Override
	public int size() {
		return mOverlays.size();
	}

	/**
	 * onTap is handled explicitly as a custom dialog box is used instead of the
	 * default alert box The dialog box is display when an overlay item is
	 * clicked on the map It displays the specified text, image and rating
	 * 
	 * @param index
	 *            - index of the overlay item that was tapped
	 */
	@Override
	protected boolean onTap(int index) {
		OverlayItem item = mOverlays.get(index);
		Dialog dialog = new Dialog(mContext);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.map_dialog_box);

		ImageView image = (ImageView) dialog.findViewById(R.id.imagedialog);
		image.setImageResource(R.drawable.androidmarker);
		TextView text = (TextView) dialog.findViewById(R.id.venuNameDialog);
		text.setText(item.getTitle());
		RatingBar ratingbarViewDialog = (RatingBar) dialog
				.findViewById(R.id.ratingbarDialog);
		String rating = "";
		if (item.getSnippet().equals("null") != true && item.getSnippet() != "")
			rating = item.getSnippet();
		else
			rating = "0.0";
		try {
			ratingbarViewDialog.setRating(Float.parseFloat(rating));
		} catch (NumberFormatException e) {
			Log.e("log_tag", "Error in parsing Rating" + e.toString());
		}
		dialog.show();
		return true;
	}

	/**
	 * draw method is overridden to be handled explicitly Circles through all
	 * the overlays and displays appropriate text at the center of the overlay
	 */
	@Override
	public void draw(android.graphics.Canvas canvas, MapView mapView,
			boolean shadow) {
		super.draw(canvas, mapView, shadow);

		if (shadow == false) {
			// cycle through all overlays
			for (int index = 0; index < mOverlays.size(); index++) {
				OverlayItem item = mOverlays.get(index);

				// Converts lat/lng-Point to coordinates on the screen
				GeoPoint point = item.getPoint();
				Point ptScreenCoord = new Point();
				mapView.getProjection().toPixels(point, ptScreenCoord);

				// Paint
				Paint paint = new Paint();
				paint.setTextAlign(Paint.Align.CENTER);
				paint.setTextSize(mTextSize);
				paint.setARGB(150, 0, 0, 0); // alpha, r, g, b (Black, semi
												// see-through)

				// Trim the title and show text to the right of the icon
				String title = "";
				if ((item.getTitle() != null || item.getTitle() != "")
						&& item.getTitle().length() >= 16)
					title = item.getTitle().substring(0, 15);
				canvas.drawText(title, ptScreenCoord.x, ptScreenCoord.y
						+ mTextSize, paint);
			}
		}
	}
}
